package com.zxx.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.zxx.ssm.dao.Teacher;
import com.zxx.ssm.dao.TeacherExample;
import com.zxx.ssm.model.biz.TeacherBiz;
import com.zxx.ssm.model.result.BizResponse;
import com.zxx.ssm.service.TeacherService;
import com.zxx.ssm.util.PaginationUtil;
import com.zxx.ssm.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("teacher")
@Controller
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("getAll")
    @ResponseBody
    public BizResponse getAll(@RequestParam String username, @RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "10") int pageSize,
                              @RequestParam(defaultValue = "username") String sort, @RequestParam(defaultValue = "desc") String order) {
        PaginationUtil.preset(pageNumber, pageSize, sort, order);
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        if (!StringUtils.isBlank(username))
            criteria.andUsernameLike(username);
        List<Teacher> teacherList = teacherService.selectByExample(teacherExample);
        PageInfo pageInfo = new PageInfo(teacherList);
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("teaList", pageInfo);
        return bizResponse;
    }

    @RequestMapping("getTeaList")
    @ResponseBody
    public BizResponse getTeaList(@RequestParam String username, @RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "10") int pageSize,
                                  @RequestParam(defaultValue = "username") String sort, @RequestParam(defaultValue = "desc") String order) {
        PaginationUtil.preset(pageNumber, pageSize, sort, order);
        List<TeacherBiz> teacherBizs = teacherService.getTeaList(username);
        PageInfo pageInfo = new PageInfo(teacherBizs);
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("teaList", pageInfo);
        return bizResponse;
    }

    @RequestMapping(value = "updateTeacher", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse updateTeacher(@ModelAttribute Teacher teacher){
        Teacher tea = teacherService.selectByPrimaryKey(teacher.getId());
        tea.setTeaDescription(teacher.getTeaDescription());
        teacherService.updateByPrimaryKey(tea);
        return new BizResponse();
    }



}
